<?php

namespace Ycr\Pack\helpers;

/**
 * 字符串的截取
 * Class Intercept
 * @package Ycr\Pack\helpers
 * @author ycr <Email: yuncran@126.com>
 */
class Intercept
{

    /**
     * @param string $string *截取的字符串
     * @param string $sub *截取的字段位置 比如 ：@ 或 *
     * @param int $type *截取类型
     * @author ycr <Email: yuncran@126.com>
     */
    public static function subString(string $string, string $sub, int $type = 2)
    {
        if ($type == 1) {
            //指定字符串最后一次出现之前的内容
            $result = substr($string, 0, strrpos($string, $sub));
        } else {
            //指定字符串最后一次出现之后的内容
            $result = substr($string, strripos($string, $sub) + 1);
        }
        return $result;
    }

    /**
     * 取小数点后两位不四舍五入
     * @param $money
     * @param $accuracy
     * @return float
     * @author ycr <Email: yuncran@126.com>
     */
    public static function number_format($money, $accuracy = 2)
    {
        $str_ret = 0;
        if (empty($money) === false) {
            $str_ret = sprintf("%." . $accuracy . "f", substr(sprintf("%." . ($accuracy + 1) . "f", floatval($money)), 0, -1));
        }

        return floatval($str_ret);
    }
}